package pkg

import (
	"context"
	"github.com/qiniu/go-sdk/v7/storagev2/credentials"
	"github.com/qiniu/go-sdk/v7/storagev2/http_client"
	"github.com/qiniu/go-sdk/v7/storagev2/uploader"
	"mime/multipart"
	"person/appconfig"
)

func Qiniuyun(file multipart.File, m *multipart.FileHeader, name string) (string, error) {
	accessKey := appconfig.ViperData.Qiniuyun.AccessKey
	secretKey := appconfig.ViperData.Qiniuyun.SecretKey
	mac := credentials.NewCredentials(accessKey, secretKey)
	bucket := appconfig.ViperData.Qiniuyun.Bucket
	key := "github-x.png"
	uploadManager := uploader.NewUploadManager(&uploader.UploadManagerOptions{
		Options: http_client.Options{
			Credentials: mac,
		},
	})
	err := uploadManager.UploadReader(context.Background(), file, &uploader.ObjectOptions{
		BucketName: bucket,
		ObjectName: &key,
		FileName:   key,
		CustomVars: map[string]string{
			"name": "github logo",
		},
	}, nil)
	return appconfig.ViperData.Qiniuyun.Url + name, err
}
